<?
			$bu_date = date("ymdhi");
			$back2 = "CREATE TABLE fic_listing_".$bu_date." AS (SELECT * FROM fic_listing)";
			//$db->Execute($back2);

$url = "http://www.tthfanfic.com/latest.php";
echo $url."<br>";
	$fdat =  file ( $url );
$getdata=false;
echo count($fdat)."<br>";
//exit();
while(list($key1,$val1)=each($fdat)){
//	if(stristr($val1,"<div class='story")){
	if(stristr($val1,"<div class='storylist")){
		$getdata=true;
	}
	if($getdata){
		$valA .="\n".$val1;
	}
}

$val1=$valA;

$valDat = explode("<div class='storylistitem '>",$val1);
while(list($key1,$val1a)=each($valDat)){
//echo htmlspecialchars($val1a)."<hr>";

	$val1 = explode('</div>',$val1a);
	while(list($keyD,$valD)=each($val1)){
		if(stristr($valD,"<div class='storytitle ")){
			//echo "+";
			$collection=$collection+1;
			//$trim1 = str_replace("title titlecolors'><a href='/Story-","",$valD);
			//$trim1 = str_replace("<a href='/Story-","",$valD);
			$trim1 = explode("<a href='/Story-",$valD);
			//var_dump($trim1);
			//$t1 = stristr($trim1,"/");
			$trim2 = explode("/",$trim1[1]);
			$dataA[$collection]['id'] = $trim2[0];
			$trim3 = explode("'>",$trim2[1]);
			$dataA[$collection]['title'] = trim(str_replace('<','',$trim3[1]));
		}elseif(stristr($valD,"<div class='storydesc'>")){
			$dataA[$collection]['summary']=trim(strip_tags(str_replace("<div class='storydesc'>","",$valD)));
		}elseif(stristr($valD,"details'>")){
			//echo htmlspecialchars($valD)."<hr>";
			$vt1 = explode('/AuthorStories-',$valD);
			//echo $vt1[1]."<hr>";
			$vt2 = explode('</a>',$vt1[1]);
			$vt3 = explode("'>",$vt2[0]);
				$aidt =explode("/", $vt3[0]);
				$dataA[$collection]['author_id'] =$aidt[0];
				$dataA[$collection]['author'] = trim(str_replace('-:-','',strip_tags($vt3[1])));
			$vt3 = explode('Chapters&nbsp;[',$vt2[1]);
			$vt4 = explode("]",$vt3[1]);
			if($collection!=0){
				$dataA[$collection]['chapters'] = trim(str_replace("' onClick='return confirm(&quot;I am old enough to read this&quot;)","",str_replace("'>",'',$vt4[0])));
			}
			$vt1 = explode("Published&nbsp;[",$valD);
			$vt2 = explode("]",$vt1[1]);
				//echo $vt2[0]."<br>";
			if($collection!=0){
				$vt3 = explode("&nbsp;",$vt2[0]);
				$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
				$dataA[$collection]['published'] = date("Y-m-d", strtotime($vt4));				
			}
			$vt1 = explode("Updated&nbsp;[",$valD);
				$vt2 = explode("]",$vt1[1]);
				//echo $vt2[0]."<br>";
			if($collection!=0){
				$vt3 = explode("&nbsp;",$vt2[0]);
				$vt4 = $vt3[1]." ".$vt3[0]." 20".$vt3[2];
				$dataA[$collection]['updated'] = date("Y-m-d", strtotime($vt4));					
			}				
		}else{
		//echo htmlspecialchars($valD)."<hr>";
		
		}

	}

}

//echo "<pre>";
//var_dump($dataA);
//exit();		
//old version

echo "&nbsp;&nbsp;&nbsp;Scan Complete<br>";
$cnt = 0; $cnt_n = 0; $cnt_u = 0; $cnt_Q = 0;
//echo "<pre>";
//var_dump($dataA);
//exit();	

	while(list($key,$val)=each($dataA)){
		$sql = "INSERT INTO `fic_listing` ( `story_id` , `author_id` , `story_title` , `chapters` , `published` , `updated` , `status`,`summary`,`inserted` ) VALUES ('".$val['id']."', '".$val['author_id']."', '".str_replace("'","''",$val['title'])."', '".$val['chapters']."', '".$val['published']."', '".$val['updated']."', '?','".str_replace("'","''",$val['summary'])."', NOW())";
		$cnt = $cnt + 1;
		$rs1 = $db->Execute($sql);
		if($rs1===false){ 
			$err = $db->ErrorMsg();
			if(stristr($err,'Duplicate entry')){

				$sqlc = "SELECT * FROM fic_listing WHERE sitekey='T' and story_id='".$val['id']."'";
				$rsC = $db->Execute($sqlc);
				$upd = false;
				if($rsC->Fields('chapters')!=$val['chapters']){ $upd = true; }
				if($rsC->Fields('updated')!=$val['updated']){ $upd = true; }
				$insid = $rsC->Fields('id');
				$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='13'");
				if($upd){
					$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
					if($rsC->Fields('published')=='0000-00-00'){
						$sqT .= ", published='".$val['published']."'";						
					}
					if($rsC->Fields('author_id')=='0'){
						$sqT .= ", author_id='".$val['author_id']."'";						
					}
					if($rsC->Fields('summary')!=$val['summary']){
						$sqT .= ", summary='".str_replace("'","''",$val['summary'])."'";	
					}	
					if($rsC->Fields('story_title')!=$val['title']){
						$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
					}	
					if($rsC->Fields('status')=='?' ){
						$cnt_Q = $cnt_Q + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";
						$db->Execute($sqlU);
					}elseif( $rsC->Fields('status')=='N'){
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')=='S'){	
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";					
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')!='I'){	
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U' WHERE sitekey='T' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')=='I'){	
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='T' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}
					//echo $sqlU."<br>";
				}
				//echo "<br>";
			}else{
				echo "<br><b>".$err."</b>".$sql."<br>";
			}
		}else{
			$insid = $db->Insert_ID();
			$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='13'");
			$cnt_n = $cnt_n + 1;
			//if new story try to insert author		
			$sql2 = "INSERT INTO `fic_authors` ( `author_id` , `author_name` )VALUES ('".$val['author_id']."', '".str_replace("'","''",$val['author'])."')";
			$rs2 = $db->Execute($sql2);
		}
	}

echo '&nbsp;&nbsp;&nbsp;Update Completed<br>';
echo '&nbsp;&nbsp;&nbsp;'.$cnt.' total,'.$cnt_n.'('.$cnt_Q.') new,'.$cnt_u.' updated<br>';

$db->Execute("UPDATE fic_authors SET linkid=id WHERE linkid='0'");

?>
